package com.easyaop.asm.bytecode.asm;

import lombok.Setter;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;

public class OffsetMethodVisitor extends MethodVisitor {
    @Setter
    private int start;
    @Setter
    private int offset;

    protected OffsetMethodVisitor(int api, MethodVisitor methodVisitor) {
        super(api, methodVisitor);
    }

    @Override
    public void visitVarInsn(int opcode, int varIndex) {
        if (start <= varIndex) {
            varIndex += offset;
        }
        super.visitVarInsn(opcode, varIndex);
    }

    @Override
    public void visitIincInsn(int varIndex, int increment) {
        if (start <= varIndex) {
            varIndex += offset;
        }
        super.visitIincInsn(varIndex, increment);
    }

    @Override
    public void visitLocalVariable(String name, String descriptor, String signature, Label start, Label end, int index) {
        if (this.start <= index) {
            index += offset;
        }
        super.visitLocalVariable(name, descriptor, signature, start, end, index);
    }
}
